<?php
include_once ('../global.php');
require_once 'css/mydata/PHPExcel/Classes/PHPExcel.php';
//第一场
//12-09


$biaoge_1=18;
$biaoge_2=30;
$all_data=[];
$objPHPExcel=new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle("利佰特-2024-12-09");
// 添加数据到Excel文件
//$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello, World!');
//构建列数--根据实际需要构建即可
$letter = array('A', 'B', 'C', 'D', 'E');
//表头数组--需和列数一致
$tableheader = array('时间', '球员1', '球员2', '球员3', '球员4');
//填充表头信息
for ($i = 0; $i < count($tableheader); $i++) {
    $objPHPExcel->getActiveSheet()->setCellValue("$letter[$i]1", "$tableheader[$i]");
}
$time_sql="select id,time from bmw_court_order_time where court_id=1 and date='2024-12-09' order by id asc";
$time_list= $db->SelectSQL($time_sql);
foreach ($time_list as $val){
    $all_data[$val['time']]=[];
    $where=" where o.is_del=0 and order_time_id=".$val['id'];
    $sql = "SELECT pl.pl_cn_name,o.name FROM `bmw_court_order` o left join player pl on o.user_id=pl.pl_id  {$where}";
    $list= $db->SelectSQL($sql);
    foreach ($list as $k =>$v){
        $all_data[$val['time']][]=$v['name']?$v['name']:$v['pl_cn_name'];
    }
}
$k_start=0;
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);//设置默认行高
foreach ($all_data as $k=>$v){
    $num = $k_start + 1+1;
    //设置每一列的内容
    $objPHPExcel->getActiveSheet()
        ->setCellValue('A' . $num, $k)
        ->setCellValue('B' . $num, $v && isset($v[0])?$v[0]:"")
        ->setCellValue('C' . $num, $v && isset($v[1])?$v[1]:"")
        ->setCellValue('D' . $num, $v && isset($v[2])?$v[2]:"")
        ->setCellValue('E' . $num, $v && isset($v[3])?$v[3]:"");
    $k_start++;
}
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth($biaoge_1);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth($biaoge_2);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth($biaoge_2);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth($biaoge_2);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth($biaoge_2);


$all_data=[];
// 创建第二个 sheet，并添加一些数据
$secondSheet = $objPHPExcel->createSheet(); // 创建新的 sheet 并获取其索引
$secondSheet->setTitle('利佰特-2024-12-10'); // 设置 sheet 的名称

//填充表头信息
for ($i = 0; $i < count($tableheader); $i++) {
    $secondSheet->setCellValue("$letter[$i]1", "$tableheader[$i]");
}
$time_sql="select id,time from bmw_court_order_time where court_id=1 and date='2024-12-10' order by id asc";
$time_list= $db->SelectSQL($time_sql);
foreach ($time_list as $val){
    $all_data[$val['time']]=[];
    $where=" where o.is_del=0 and order_time_id=".$val['id'];
    $sql = "SELECT pl.pl_cn_name,o.name FROM `bmw_court_order` o left join player pl on o.user_id=pl.pl_id  {$where}";
    $list= $db->SelectSQL($sql);
    foreach ($list as $k =>$v){
        $all_data[$val['time']][]=$v['name']?$v['name']:$v['pl_cn_name'];
    }
}
$k_start=0;
$secondSheet->getDefaultRowDimension()->setRowHeight(20);//设置默认行高
foreach ($all_data as $k=>$v){
    $num = $k_start + 1+1;
    //设置每一列的内容
    $secondSheet
        ->setCellValue('A' . $num, $k)
        ->setCellValue('B' . $num, isset($v[0])?$v[0]:"")
        ->setCellValue('C' . $num, isset($v[1])?$v[1]:"")
        ->setCellValue('D' . $num, isset($v[2])?$v[2]:"")
        ->setCellValue('E' . $num, isset($v[3])?$v[3]:"");
    $k_start++;
}

$secondSheet->getColumnDimension('A')->setWidth($biaoge_1);
$secondSheet->getColumnDimension('B')->setWidth($biaoge_2);
$secondSheet->getColumnDimension('C')->setWidth($biaoge_2);
$secondSheet->getColumnDimension('D')->setWidth($biaoge_2);
$secondSheet->getColumnDimension('E')->setWidth($biaoge_2);



$all_data=[];
// 创建第三个 sheet，并添加一些数据
$thirdSheet = $objPHPExcel->createSheet(); // 创建新的 sheet 并获取其索引
$thirdSheet->setTitle('诺曼-2024-12-09'); // 设置 sheet 的名称

//填充表头信息
for ($i = 0; $i < count($tableheader); $i++) {
    $thirdSheet->setCellValue("$letter[$i]1", "$tableheader[$i]");
}
$time_sql="select id,time from bmw_court_order_time where court_id=2 and date='2024-12-09' order by id asc";
$time_list= $db->SelectSQL($time_sql);
foreach ($time_list as $val){
    $all_data[$val['time']]=[];
    $where=" where o.is_del=0 and order_time_id=".$val['id'];
    $sql = "SELECT pl.pl_cn_name,o.name FROM `bmw_court_order` o left join player pl on o.user_id=pl.pl_id  {$where}";
    $list= $db->SelectSQL($sql);
    foreach ($list as $k =>$v){
        $all_data[$val['time']][]=$v['name']?$v['name']:$v['pl_cn_name'];
    }
}
$k_start=0;
$thirdSheet->getDefaultRowDimension()->setRowHeight(20);//设置默认行高
foreach ($all_data as $k=>$v){
    $num = $k_start + 1+1;
    //设置每一列的内容
    $thirdSheet
        ->setCellValue('A' . $num, $k)
        ->setCellValue('B' . $num, isset($v[0])?$v[0]:"")
        ->setCellValue('C' . $num, isset($v[1])?$v[1]:"")
        ->setCellValue('D' . $num, isset($v[2])?$v[2]:"")
        ->setCellValue('E' . $num, isset($v[3])?$v[3]:"");
    $k_start++;
}

$thirdSheet->getColumnDimension('A')->setWidth($biaoge_1);
$thirdSheet->getColumnDimension('B')->setWidth($biaoge_2);
$thirdSheet->getColumnDimension('C')->setWidth($biaoge_2);
$thirdSheet->getColumnDimension('D')->setWidth($biaoge_2);
$thirdSheet->getColumnDimension('E')->setWidth($biaoge_2);



$all_data=[];
// 创建第四个 sheet，并添加一些数据
$fourSheet = $objPHPExcel->createSheet(); // 创建新的 sheet 并获取其索引
$fourSheet->setTitle('诺曼-2024-12-10'); // 设置 sheet 的名称

//填充表头信息
for ($i = 0; $i < count($tableheader); $i++) {
    $fourSheet->setCellValue("$letter[$i]1", "$tableheader[$i]");
}
$time_sql="select id,time from bmw_court_order_time where court_id=2 and date='2024-12-10' order by id asc";
$time_list= $db->SelectSQL($time_sql);
foreach ($time_list as $val){
    $all_data[$val['time']]=[];
    $where=" where o.is_del=0 and order_time_id=".$val['id'];
    $sql = "SELECT pl.pl_cn_name,o.name FROM `bmw_court_order` o left join player pl on o.user_id=pl.pl_id  {$where}";
    $list= $db->SelectSQL($sql);
    foreach ($list as $k =>$v){
        $all_data[$val['time']][]=$v['name']?$v['name']:$v['pl_cn_name'];
    }
}
$k_start=0;
$fourSheet->getDefaultRowDimension()->setRowHeight(20);//设置默认行高
foreach ($all_data as $k=>$v){
    $num = $k_start + 1+1;
    //设置每一列的内容
    $fourSheet
        ->setCellValue('A' . $num, $k)
        ->setCellValue('B' . $num, isset($v[0])?$v[0]:"")
        ->setCellValue('C' . $num, isset($v[1])?$v[1]:"")
        ->setCellValue('D' . $num, isset($v[2])?$v[2]:"")
        ->setCellValue('E' . $num, isset($v[3])?$v[3]:"");
    $k_start++;
}

$fourSheet->getColumnDimension('A')->setWidth($biaoge_1);
$fourSheet->getColumnDimension('B')->setWidth($biaoge_2);
$fourSheet->getColumnDimension('C')->setWidth($biaoge_2);
$fourSheet->getColumnDimension('D')->setWidth($biaoge_2);
$fourSheet->getColumnDimension('E')->setWidth($biaoge_2);
















$excel_name="预约试场";
ob_end_clean();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$excel_name.'.xlsx"');
header('Cache-Control: max-age=0');

// 保存并输出Excel文件
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');

?>